using System.IO;
using FireStarter.Base;
using FireStarterModeller.BO;
using Habanero.BO;

namespace FireStarterModeller.Logic
{
    /// <summary>
    /// Creatss a Solution with all the standard Control Types, Control mapper types and Property Types.
    /// </summary>
    public class SolutionCreator
    {
        /// <summary>
        /// Creates a Solution with all the standard Control Types, Control mapper types and Property Types.
        /// </summary>
        public DMSolution CreateSolution()
        {
            string solutionName = DMSolution.DEFAULT_NEW_SOLUTION_NAME;
            solutionName = GetSolutionName(solutionName);
            DMSolution solution = new DMSolution(solutionName);
            solution.SolutionDirectory = @"C:\";
            UpdateNewSolution(solution);

            CreateDefaultAssembly(solution);
            solution.Save();
            return solution;
        }

        /// <summary>
        /// Creates a Solution with all the standard Control Types, Control mapper types and Property Types.
        /// </summary>
        public DMSolution CreateSolution(string fileName)
        {
            DMSolution dmSolution = new DMSolution(Path.GetFileNameWithoutExtension(fileName));
            CreateDefaultAssembly(dmSolution);
            UpdateNewSolution(dmSolution);
            return dmSolution;
        }

        public DMAssembly CreateDefaultAssembly(DMSolution dmSolution)
        {
            DMAssembly defaultAssembly = dmSolution.CreateAssembly(dmSolution.SolutionName + SolutionRenamer.DefaultAssemblyBOSuffix);
            dmSolution.DefaultAssembly = defaultAssembly;
            return defaultAssembly;
        }

        private void CreateStandardControlMapperTypes(DMSolution solution)
        {
            CreateControlMapperType(solution, BOBroker.GetMapperAssemblyName(), "AutoLoadingRelationshipComboBoxMapper");
            CreateControlMapperType(solution, BOBroker.GetMapperAssemblyName(), "TextBoxMapper");
        }

        private UIControlMapperType CreateControlMapperType(DMSolution solution, string assemblyName, string typeName)
        {
            UIControlMapperType type = solution.UIControlMapperTypes.CreateBusinessObject();

            type.AssemblyName = assemblyName;
            type.TypeName = typeName;
            return type;
        }

        public void UpdateNewSolution(DMSolution solution)
        {
            CreateStandardPropTypes(solution);
            CreateStandardGridColumnControlTypes(solution);
            CreateStandardControlTypes(solution);
            CreateStandardControlMapperTypes(solution);
            CreateStandardFilterControlTypes(solution);
        }

        private string GetSolutionName(string solutionName)
        {
            BusinessObjectCollection<DMSolution> col = new BusinessObjectCollection<DMSolution>();
            col.Load("SolutionName = " + solutionName, "");
            if (col.Count > 0)
            {
                solutionName += "1";
                return GetSolutionName(solutionName);
            }
            return solutionName;
        }

        private static void CreateStandardFilterControlTypes(DMSolution solution)
        {
            CreateFilterControlType(solution, BOBroker.GetMapperAssemblyName(), "BoolCheckBoxFilter");
            CreateFilterControlType(solution, BOBroker.GetMapperAssemblyName(), "DateRangeComboBoxFilter");
            CreateFilterControlType(solution, BOBroker.GetMapperAssemblyName(), "DateTimePickerFilter");
            CreateFilterControlType(solution, BOBroker.GetMapperAssemblyName(), "StringComboBoxFilter");
            CreateFilterControlType(solution, BOBroker.GetMapperAssemblyName(), "StringTextBoxFilter");
        }

        private static UITypeDefinition CreateFilterControlType
            (DMSolution solution, string assemblyName, string typeName)
        {
            UITypeDefinition type = solution.UIFilterControlTypes.CreateBusinessObject();

            type.AssemblyName = assemblyName;
            type.TypeName = typeName;
            return type;
        }

        private static void CreateStandardControlTypes(DMSolution solution)
        {
            CreateControlType(solution, "System.Windows.Forms", "CheckBox");
            CreateControlType(solution, "System.Windows.Forms", "TextBox");
            CreateControlType(solution, "System.Windows.Forms", "ComboBox");
            CreateControlType(solution, "System.Windows.Forms", "DateTimePicker");
        }

        private static void CreateStandardGridColumnControlTypes(DMSolution solution)
        {
            CreateColumnControlType(solution, "System.Windows.Forms", "DataGridViewCheckBoxColumn");
            CreateColumnControlType(solution, "System.Windows.Forms", "DataGridViewTextBoxColumn");
            CreateColumnControlType(solution, "System.Windows.Forms", "DataGridViewComboBoxColumn");
        }


        private static UIGridColumnControlType CreateColumnControlType
            (DMSolution solution, string assemblyName, string typeName)
        {
            UIGridColumnControlType type = solution.UIGridColumnControlTypes.CreateBusinessObject();

            type.AssemblyName = assemblyName;
            type.TypeName = typeName;
            return type;
        }

        private static UIControlType CreateControlType(DMSolution solution, string assemblyName, string typeName)
        {
            UIControlType type = solution.UIControlTypes.CreateBusinessObject();

            type.AssemblyName = assemblyName;
            type.TypeName = typeName;
            return type;
        }

        private static void CreateStandardPropTypes(DMSolution solution)
        {
            DMPropertyType.FindOrCreate(solution, "System", "String");
            DMPropertyType.FindOrCreate(solution, "System", "Int32");
            DMPropertyType.FindOrCreate(solution, "System", "Guid");
            DMPropertyType.FindOrCreate(solution, "System", "Single");
            DMPropertyType.FindOrCreate(solution, "System", "Boolean");
            DMPropertyType.FindOrCreate(solution, "System", "Decimal");
            DMPropertyType.FindOrCreate(solution, "System", "DateTime");
            DMPropertyType.FindOrCreate(solution, "System", "TimeSpan");
            DMPropertyType.FindOrCreate(solution, "System", "Double");
            DMPropertyType.FindOrCreate(solution, "System", "Int64");
        }

        

        
    }
}